import Floor from './Floor'

/**
 * @class 地板管理类
 * 1. 增加地板
 * 2. 删除地板
 * 3. 获取地板
 */
export default class MapFloor extends Sprite {

  constructor() {
    super();

    this.dieFloorList = [];
    this.init();
  }

  init() {

    const firstFloor = this.addFloor(1);
    firstFloor.pos(0, Config.deviceHeight - firstFloor.height);// 第一个地板位置
    Laya.timer.frameLoop(1, this, this.onLoop);
  }

  onLoop() {

    if(gamePauseHandle(this, this.onLoop)) return ;

    // 是否有需要移除的面板
    while(this.dieFloorList.length > 0) {
      const floor = this.dieFloorList.shift();
      floor.removeSelf();
      floor.itemList.forEach(item => {
        Pool.recover('item', item);
        item.removeSelf();
      });
      Pool.recover('floor', floor);
    }
  }

  addFloor(type) {

    const floor = Pool.getItemByClass('floor', Floor);
    floor.init(type);
    floor.once(Floor.OUT_COMPLETE, this, this.getFloor);// 注册事件，只触发一次
    floor.once(Floor.OUT_DIE, this, this.delFloor);
    this.addChild(floor);

    return floor;
  }

  getFloor() {

    this.addFloor(2);
  }

  delFloor(floor) {

    this.dieFloorList.push(floor);
  }
}